package com.summer.data.table.column;

import com.summer.data.logging.Logger;
import com.summer.data.logging.LoggerFactory;
import com.summer.data.table.BaseColumn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * 日期 + 时间
 * 
 * @author 345480567@qq.com
 */

public class DateTimeColumn extends BaseColumn<LocalDateTime> {

    private static Logger logger = LoggerFactory.getLogger(DateTimeColumn.class);

    public DateTimeColumn(String colName) {
        super(colName);
    }

    @Override
    public void fetchNotNullValue(ResultSet resultSet, Integer index) throws SQLException {
        value = getValue(resultSet, index);
    }

    public void generateWithCurrent() {
        setValue(LocalDateTime.now());
    }

    public static LocalDateTime getValue(ResultSet resultSet, Integer index) throws SQLException {
        LocalDateTime value = null;
        Object object = resultSet.getObject(index);
        if (object != null) {
            if (object instanceof LocalDateTime) {
                value = (LocalDateTime) object;
            } else {
                logger.warn("时间类型不匹配: db:%s  -> java:%s",
                        new Object[] { object.getClass().getName(), Date.class.getName() });
            }
        }
        return value;
    }

}
